home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH15 / EX15_4.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-03-25  |  5.1 KB  |  296 lines

  1. ; EX15_4.asm
  2. ;
  3. ; This program compares the performance of length prefixed strings versus
  4. ; zero terminated strings using some simple examples.
  5. ;
  6. ; Note: these routines all assume that the strings are in the data segment
  7. ;       and both ds and es already point into the data segment.
  8.  
  9.         .386
  10.         option        segment:use16
  11.  
  12.         include     stdlib.a
  13.         includelib    stdlib.lib
  14.  
  15.  
  16. dseg        segment    para public 'data'
  17.  
  18. LStr1        byte    17,"This is a string."
  19. LResult        byte    256 dup (?)
  20.  
  21. ZStr1        byte    "This is a string",0
  22. ZResult        byte    256 dup (?)
  23.  
  24. dseg        ends
  25.  
  26.  
  27. cseg        segment    para public 'code'
  28.         assume    cs:cseg, ds:dseg
  29.  
  30.  
  31. ; LStrCpy: Copies a length prefixed string pointed at by SI to 
  32. ;          the length prefixed string pointed at by DI.
  33.  
  34. LStrCpy        proc
  35.         push    si
  36.         push    di
  37.         push    cx
  38.  
  39.         cld
  40.  
  41.         mov    cl, [si]    ;Get length of string.
  42.         mov    ch, 0
  43.         inc    cx        ;Include length byte.
  44.     rep    movsb
  45.  
  46.         pop    cx
  47.         pop    di
  48.         pop    si
  49.         ret
  50. LStrCpy        endp
  51.  
  52.  
  53.  
  54. ; LStrCat-    Concatenates the string pointed at by SI to the end
  55. ;        of the string pointed at by DI using length
  56. ;        prefixed strings.
  57.  
  58. LStrCat        proc
  59.         push    si
  60.         push    di
  61.         push    cx
  62.  
  63.         cld
  64.  
  65. ; Compute the final length of the concatenated string
  66.  
  67.         mov    cl, [di]        ;Get orig length.
  68.         mov    ch, [si]        ;Get 2nd Length.
  69.         add    [di], ch        ;Compute new length.
  70.  
  71. ; Move SI to the first byte beyond the end of the first string.
  72.  
  73.         mov    ch, 0            ;Zero extend orig len.
  74.         add    di, cx            ;Skip past str.
  75.         inc    di            ;Skip past length byte.
  76.  
  77. ; Concatenate the second string (SI) to the end of the first string (DI)
  78.  
  79.     rep    movsb                ;Copy 2nd to end of orig.
  80.  
  81.         pop    cx
  82.         pop    di
  83.         pop    si
  84.         ret
  85. LStrCat        endp
  86.         
  87.  
  88.  
  89. ; LStrCmp-    String comparison using two length prefixed strings.
  90. ;        SI points at the first string, DI points at the
  91. ;        string to compare it against.
  92.  
  93. LStrCmp        proc
  94.         push    si
  95.         push    di
  96.         push    cx
  97.  
  98.         cld
  99.  
  100. ; When comparing the strings, we need to compare the strings
  101. ; up to the length of the shorter string.  The following code
  102. ; computes the minimum length of the two strings.
  103.  
  104.         mov    cl, [si]    ;Get the minimum of the two lengths
  105.         mov    ch, [di]
  106.         cmp    cl, ch
  107.         jb    HasMin
  108.         mov    cl, ch
  109. HasMin:        mov    ch, 0
  110.  
  111.     repe    cmpsb            ;Compare the two strings.
  112.         je    CmpLen
  113.         pop    cx
  114.         pop    di
  115.         pop    si
  116.         ret
  117.  
  118. ; If the strings are equal through the length of the shorter string,
  119. ; we need to compare their lengths
  120.  
  121. CmpLen:        pop    cx
  122.         pop    di
  123.         pop    si
  124.  
  125.         mov    cl, [si]
  126.         cmp    cl, [di]
  127.         ret
  128. LStrCmp        endp
  129.         
  130.  
  131. ; ZStrCpy- Copies the zero terminated string pointed at by SI
  132. ;          to the zero terminated string pointed at by DI.
  133.  
  134. ZStrCpy        proc
  135.         push    si
  136.         push    di
  137.         push    ax
  138.  
  139. ZSCLp:        mov    al, [si]
  140.         inc    si
  141.         mov    [di], al
  142.         inc    di
  143.         cmp    al, 0
  144.         jne    ZSCLp
  145.  
  146.         pop    ax
  147.         pop    di
  148.         pop    si
  149.         ret
  150. ZStrCpy        endp
  151.  
  152.  
  153. ; ZStrCat-    Concatenates the string pointed at by SI to the end
  154. ;        of the string pointed at by DI using zero terminated
  155. ;        strings.
  156.  
  157. ZStrCat        proc
  158.         push    si
  159.         push    di
  160.         push    cx
  161.         push    ax
  162.  
  163.         cld
  164.  
  165. ; Find the end of the destination string:
  166.  
  167.         mov    cx, 0FFFFh
  168.         mov    al, 0        ;Look for zero byte.
  169.     repne    scasb
  170.  
  171. ; Copy the source string to the end of the destination string:
  172.  
  173. ZcatLp:        mov    al, [si]
  174.         inc    si
  175.         mov    [di], al
  176.         inc    di
  177.         cmp    al, 0
  178.         jne    ZCatLp
  179.  
  180.         pop    ax
  181.         pop    cx
  182.         pop    di
  183.         pop    si
  184.         ret
  185. ZStrCat        endp
  186.         
  187.  
  188. ; ZStrCmp-    Compares two zero terminated strings.
  189. ;        This is actually easier than the length
  190. ;        prefixed comparison.
  191.  
  192. ZStrCmp        proc
  193.         push    cx
  194.         push    si
  195.         push    di
  196.  
  197. ; Compare the two strings until they are not equal
  198. ; or until we encounter a zero byte.  They are equal
  199. ; if we encounter a zero byte after comparing the
  200. ; two characters from the strings.
  201.  
  202. ZCmpLp:        mov    al, [si]
  203.         inc    si
  204.         cmp    al, [di]
  205.         jne    ZCmpDone
  206.         inc    di
  207.         cmp    al, 0
  208.         jne    ZCmpLp
  209.  
  210. ZCmpDone:    pop    di
  211.         pop    si
  212.         pop    cx
  213.         ret
  214. ZStrCmp        endp
  215.  
  216.  
  217. Main        proc
  218.         mov    ax, dseg
  219.         mov    ds, ax
  220.         mov    es, ax
  221.         meminit
  222.  
  223.  
  224.  
  225.         print
  226.         byte    "The following code does 1,000,000 string "
  227.         byte    "operations using",cr,lf
  228.         byte    "length prefixed strings.  Measure the amount "
  229.         byte    "of time this code",cr,lf
  230.         byte    "takes to run.",cr,lf,lf
  231.         byte    "Press any key to begin:",0
  232.  
  233.         getc
  234.         putcr
  235.  
  236.         mov    edx, 1000000
  237. LStrCpyLp:    lea    si, LStr1
  238.         lea    di, LResult
  239.         call    LStrCpy
  240.         call    LStrCat
  241.         call    LStrCat
  242.         call    LStrCat
  243.         call    LStrCpy
  244.         call    LStrCmp
  245.         call    LStrCat
  246.         call    LStrCmp
  247.  
  248.         dec    edx
  249.         jne    LStrCpyLp
  250.  
  251.  
  252.         print
  253.         byte    "The following code does 1,000,000 string "
  254.         byte    "operations using",cr,lf
  255.         byte    "zero terminated strings.  Measure the amount "
  256.         byte    "of time this code",cr,lf
  257.         byte    "takes to run.",cr,lf,lf
  258.         byte    "Press any key to begin:",0
  259.  
  260.         getc
  261.         putcr
  262.  
  263.         mov    edx, 1000000
  264. ZStrCpyLp:    lea    si, ZStr1
  265.         lea    di, ZResult
  266.         call    ZStrCpy
  267.         call    ZStrCat
  268.         call    ZStrCat
  269.         call    ZStrCat
  270.         call    ZStrCpy
  271.         call    ZStrCmp
  272.         call    ZStrCat
  273.         call    ZStrCmp
  274.  
  275.  
  276.  
  277.         dec    edx
  278.         jne    ZStrCpyLp
  279.  
  280.  
  281.  
  282.  
  283. Quit:        ExitPgm            ;DOS macro to quit program.
  284. Main        endp
  285.  
  286. cseg        ends
  287.  
  288. sseg        segment    para stack 'stack'
  289. stk        db    1024 dup ("stack   ")
  290. sseg        ends
  291.  
  292. zzzzzzseg    segment    para public 'zzzzzz'
  293. LastBytes    db    16 dup (?)
  294. zzzzzzseg    ends
  295.         end    Main
  296.